<?php
class College extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->helper('url');

        if (file_exists('application/controllers/install.php'))
            redirect('install');

        $this->load->library('session');
        $this->load->model('college_model');

        if (!($this->session->userdata['login'])) {
            redirect('poster_admin/login');
        }

    }

    function index() {
        $data['query'] = $this->college_model->getAll();
        $data['title'] = '学院列表';
        $this->load->view('templates/poster_admin/header', $data);
        $this->load->view('poster_admin/college/index', $data);
        $this->load->view('templates/poster_admin/footer', $data);
    }

    function create() {
        $this->load->helper('form');
        $this->load->library('form_validation');

        $this->form_validation->set_rules('name', '学院', 'required|is_unique[pr_college.name]');
        $data['title'] = '添加学院';

        if ($this->form_validation->run() == FALSE) {
            $this->load->view('templates/poster_admin/header', $data);
            $this->load->view('poster_admin/college/create', $data);
            $this->load->view('templates/poster_admin/footer', $data);
        }
        else {
            $this->college_model->insert();
            redirect('poster_admin/college/', 'refresh');
        }
    }

    function edit() {
        $this->load->helper('form');
        $this->load->library('form_validation');

        $this->form_validation->set_rules('name', '学院', 'required');
        $data['title'] = '修改学院';

        if ($this->form_validation->run() == FALSE) {
            $id = $this->input->get('id');
            $data['query'] = $this->college_model->select($id);
            if (empty($data['query'])) { // if $id not existed, redirect
            $data['title'] = '出错啦';
            $data['words'] = '查询id错误';
            $this->load->view('templates/poster_admin/header', $data);
            $this->load->view('poster_admin/404', $data);
            $this->load->view('templates/poster_admin/footer', $data);
            } else {
            $this->load->view('templates/poster_admin/header', $data);
            $this->load->view('poster_admin/college/edit', $data);
            $this->load->view('templates/poster_admin/footer', $data);
            }
        }
        else {
            $this->college_model->edit();
            redirect('poster_admin/college/', 'refresh');
        }
    }

    function delete() {
        $this->college_model->delete();
        redirect('poster_admin/college/', 'refresh');
    }
}